package com.wangzhenyu.common.base

import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.widget.RelativeLayout
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.RomUtils
import com.blankj.utilcode.util.ToastUtils
import com.wangzhenyu.common.R
import com.wangzhenyu.common.base.delegate.DefaultLoadingDelegate
import com.wangzhenyu.common.base.delegate.DefaultPageLoadingDelegate
import com.wangzhenyu.common.base.delegate.LoadingDelegate
import com.wangzhenyu.common.base.delegate.PageLoadingDelegate
import com.wangzhenyu.common.base.delegate.ToolBarDelegate
import com.wangzhenyu.common.view.EmptyLayout

abstract class BaseActivity<P : BasePresenter> : AppCompatActivity(), BaseView {

    //对应的Activity
    lateinit var context: AppCompatActivity

    //表示整个布局
    protected var rlBaseRoot: RelativeLayout? = null

    //这个是自定义的ToolBar
    protected var tlBar: Toolbar? = null

    //没有数据时显示的EmptyLayout
    protected var elEmpty: EmptyLayout? = null

    //对ToolBar的处理
    protected var mToolBarDelegate: ToolBarDelegate? = null

    //LoadingDialog
    protected var mLoadingDelegate: LoadingDelegate? = null

    //页面内 Loading 操作代理
    protected var mPageLoadingDelegate: PageLoadingDelegate? = null

    protected var mPresenter: P? = null
    protected var savedInstanceState: Bundle? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        this.savedInstanceState = savedInstanceState
        context = this

        initBundles()
        if (useBaseLayout()) {
            setContentView(R.layout.activity_base)
        } else {
            if (getLayoutId() != 0) {
                setContentView(getLayoutId())
            }
        }

        mPresenter = createPresenter()

        initSelf()
        addContentView()
        initView()

    }

    private fun initSelf() {
        rlBaseRoot = findViewById(R.id.rl_base_root)
        tlBar = findViewById(R.id.tl_bar)
        elEmpty = findViewById(R.id.el_empty)
        mToolBarDelegate = ToolBarDelegate(this, tlBar, useLightMode())
    }

    private fun addContentView() {
        if (getLayoutId() != 0 && useBaseLayout()) {
            layoutInflater.inflate(getLayoutId(), findViewById(R.id.container), true)
        }
    }

    /**
     * DialogLoading
     */
    protected open fun getLoadingDelegate(): LoadingDelegate? {
        if (mLoadingDelegate == null) {
            mLoadingDelegate = DefaultLoadingDelegate(this)
        }
        return mLoadingDelegate
    }

    /**
     * 页面 Loading
     */
    protected open fun getPageLoadingDelegate(): PageLoadingDelegate? {
        if (mPageLoadingDelegate == null) {
            mPageLoadingDelegate = DefaultPageLoadingDelegate(this)
            mPageLoadingDelegate!!.setPageLoadingLayout(elEmpty!!)
        }
        return mPageLoadingDelegate
    }

    override fun onDestroy() {
        super.onDestroy()

    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }

    //region 页面内等待操作
    override fun showPageLoading() {
        getPageLoadingDelegate()!!.showPageLoading()
    }

    override fun hidePageLoading() {
        getPageLoadingDelegate()!!.hidePageLoading()
    }

    override fun showNoData() {
        getPageLoadingDelegate()!!.showNoData()
    }

    override fun showNoNetwork() {
        getPageLoadingDelegate()!!.showNoNetwork()
    }

    override fun showDataError() {
        getPageLoadingDelegate()!!.showDataError()
    }

    override fun showReason(text: String) {
        getPageLoadingDelegate()!!.showNoNetwork()
    }
    //endregion

    override fun showDialogLoading() {
        getLoadingDelegate()!!.showDialogLoading(getString(R.string.loading))
    }

    override fun showDialogLoading(content: String?) {
        getLoadingDelegate()!!.showDialogLoading(content)
    }

    override fun hideDialogLoading() {
        getLoadingDelegate()!!.hideDialogLoading()
    }

    override fun showToast(content: String?) {

        if (!TextUtils.isEmpty(content)) {
            val toast = ToastUtils.make()
            if (RomUtils.isXiaomi()) {
                toast.setBgColor(ContextCompat.getColor(context, R.color.color_CC000000))
            } else {
                toast.setBgResource(R.drawable.shape_toast_bg)
            }
            toast.setGravity(Gravity.CENTER, 0, 100)
            toast.setTextColor(ContextCompat.getColor(context, R.color.common_ffffff))
            toast.show(content)
        }
    }

    override fun showBottomToast(content: String?) {
        if (!TextUtils.isEmpty(content)) {
            val toast = ToastUtils.make()
            if (RomUtils.isXiaomi()) {
                toast.setBgColor(ContextCompat.getColor(context, R.color.color_CC000000))
            } else {
                toast.setBgResource(R.drawable.shape_toast_bg)
            }
            toast.setGravity(Gravity.BOTTOM, 0, 100)
            toast.setTextColor(ContextCompat.getColor(context, R.color.common_ffffff))
            toast.show(content)
        }
    }


    // ------------------- Override Method start ------------------


    protected open fun initBundles() {}

    /**
     * 是否使用基类里面的布局，
     *
     * @return true：将子类的布局作为 View 添加到父容器中。
     * @implNote 返回false时，并且使用showPageLoading时要在自定义布局中加入empty view
     */
    protected open fun useBaseLayout() = true

    @LayoutRes
    protected abstract fun getLayoutId(): Int

    protected abstract fun createPresenter(): P


    /**
     * true : 底部白色
     * false: 主题色
     *
     * @return 默认为 true
     */
    open fun useLightMode() = true


    protected abstract fun initView()
}